package cn.hs.servlet;

import cn.hs.dao.AdminDao;
import cn.hs.dao.impl.AdminDaoImpl;
import cn.hs.entity.Admin;
import cn.hs.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        AdminDao adminDao = new AdminDaoImpl();
        Admin admin = adminDao.selectAdminByUsernameAndPassword(username, password);
        if (admin==null){//用户名或密码错误
            Result error = Result.error();
            String s = JSON.toJSONString(error);
            resp.getWriter().write(s);
        }else {
            Result success = Result.success();
            String s = JSON.toJSONString(success);
            resp.getWriter().write(s);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
